package com.atguigu.gulimail.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimail.auth.feign.MemberFeignService;
import com.atguigu.gulimail.auth.vo.GiteeAccessToken;
import com.atguigu.gulimail.auth.vo.GiteeMemberVo;
import com.atguigu.gulimail.auth.vo.GiteeUserInfo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fangy
 * @date 2022-10-08 20:01
 */
@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/oauth2.0/gitee/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {

        Map<String, String> map = new HashMap<>();

        map.put("grant_type","authorization_code");
        map.put("client_id","696b535c91a12d4d4138fe6cc4cd1237b5981c0426d2f5a1103b83126c142831");
        map.put("client_secret","7b220b4d1dddb518aad67c27ac89d411a7b106b678e6258371ee79c1caa0b984");
        map.put("code",code);
        map.put("redirect_uri","http://auth.gulimail.com/oauth2.0/gitee/success");

        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), new HashMap<>(), map);

        if(response.getStatusLine().getStatusCode() == 200){

            String tostring = EntityUtils.toString(response.getEntity());
            GiteeAccessToken giteeAccessToken = JSON.parseObject(tostring, GiteeAccessToken.class);

            Map<String,String> query = new HashMap<>();
            query.put("access_token", giteeAccessToken.getAccess_token());

            HttpResponse response_userInfo = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", new HashMap(), query);
            String giteeUserInfoJson = EntityUtils.toString(response_userInfo.getEntity());
            GiteeUserInfo giteeUserInfo = JSON.parseObject(giteeUserInfoJson, GiteeUserInfo.class);

            GiteeMemberVo giteeMember = new GiteeMemberVo();
            giteeMember.setId(giteeUserInfo.getId());
            giteeMember.setAccess_token(giteeAccessToken.getAccess_token());
            giteeMember.setExpires_in(giteeAccessToken.getExpires_in());
            giteeMember.setName(giteeUserInfo.getName());
            giteeMember.setEmaill(giteeUserInfo.getEmail());

            R r = memberFeignService.GiteeSaveOrRegit(giteeMember);
            if (r.getCode() == 0){
                MemberRespVo data = r.getData(new TypeReference<MemberRespVo>() {
                });

                session.setAttribute("loginUser",data);

                return "redirect:http://gulimail.com";
            }else {
                return "redirect:http://auth.gulimail.com/login.html";
            }

        }else {
            return "redirect:http://auth.gulimail.com/login.html";
        }

    }

}
